package tt.dz.dao.ext.system;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;
import tt.dz.dao.base.OmsCommonDao;
import tt.dz.entity.exception.OmsDaoException;
import tt.dz.entity.system.OmsOperRole;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * 操作员角色数据操作Dao
 * @author Administrator
 * @date 2015年12月16日
 */
@Repository
public class OmsOperRoleDao extends OmsCommonDao<OmsOperRole> {

	private Log log = LogFactory.getLog(OmsOperRoleDao.class);
	
	/**
	 * 通过operid查询角色关联信息，含角色对象
	 * @author Administrator
	 * @param oper_id
	 * @return
	 */
	public List<OmsOperRole> findValidOperRoleByOperId(String oper_id){
		
		OmsOperRole operRole = new OmsOperRole();
		//设置查询条件
		operRole.setOper_id(oper_id);
		operRole.setValid_flag(1);
		return super.findExample(operRole, null, null, null, null);
	}
	
	public void assignRolsToOper(final String userid, final String[] list_roleid) throws OmsDaoException
	{
		try
		{
			String sql = "insert into oms_oper_role(oper_id,role_id,valid_flag,create_time,update_time,create_oper_id,update_oper_id) values(?,?,1,now(),now(),'-1','-1')";
			this.getJdbcTemplate().batchUpdate(sql,new BatchPreparedStatementSetter() 
			{
				
				@Override
				public void setValues(PreparedStatement arg0, int i) throws SQLException
				{
					int index = 1;
					arg0.setString(index++, userid);
					arg0.setString(index++, list_roleid[i]);
				}
				
				@Override
				public int getBatchSize() {
					return list_roleid.length;
				}
			});
		}
		catch(Exception e)
		{
			log.error(e.getMessage(), e);
			throw new OmsDaoException(e);
		}
	}
	
//	public void addOperRoleIds(final String[] roleIds,final String userId) throws OmsDaoException
//	{
//		try
//		{
//			String sql = "insert into oms_oper_role(ms_manager_id,role_id,valid_flag,create_time,update_time,create_oper_id,update_oper_id) values(?,?,1,now(),now(),'-1','-1')";
//			this.getJdbcTemplate().batchUpdate(sql,new BatchPreparedStatementSetter()
//			{
//
//				@Override
//				public void setValues(PreparedStatement arg0, int arg1) throws SQLException
//				{
//					int index = 1;
//					arg0.setString(index++, userId);
//					arg0.setString(index++, roleIds[arg1]);
//				}
//
//				@Override
//				public int getBatchSize() {
//					return roleIds.length;
//				}
//			});
//		}
//		catch(Exception e)
//		{
//			throw new OmsDaoException(e);
//		}
//	}
	
	public void deleteOperRoleIds(String userId) throws OmsDaoException
	{
		try
		{
			String sql = "delete from oms_oper_role where oper_id = ?";
			Object[] args ={userId};
			this.getJdbcTemplate().update(sql, args);
		}
		catch(Exception e)
		{
			throw new OmsDaoException(e);
		}
	}
	
	public void addRoleUserMapping(String userId,int type) throws OmsDaoException
	{
		try
		{
			String sql = "insert into oms_oper_role (role_id,oper_id,valid_flag,create_time,create_oper_id,update_time,update_oper_id)		select role_id,? as oper_id,1 as valid_flag,now() as create_time, '-1' as create_oper_id,now() as update_time,'-1' as update_oper_id from oms_role 	 where entity_type = ?";
			this.getJdbcTemplate().update(sql, new Object[]{userId,type});
		}
		catch(Exception e)
		{
			throw new OmsDaoException(e);
		}
	}
	
}